#include <iostream>
#include <assert.h>
#include <string.h>

using namespace std;

namespace my_string
{
    class string 
    {
        friend ostream& operator<<(ostream& out, const string& s);
        friend istream& operator>>(istream& in, string& s);
    public:
        typedef char* iterator;
        typedef const char* const_iterator;
        typedef string& Ref;
        typedef const string& C_Ref;

        iterator begin() { return _str; }
        iterator end() { return _str + _size; }
        const_iterator begin() const { return _str; }
        const_iterator end() const { return _str + _size; }

        char* c_str() { return _str; }
        const char* c_str() const { return _str; }

        void swap(string& s)
        {
            ::swap(_str, s._str);
            ::swap(_size, s._size);
            ::swap(_capacity, s._capacity);
        }

        string(const char* str = "")
        {
            size_t len = strlen(str);
            _str = new char[len + 1];
            strcpy(_str, str);
            _size = _capacity = len;
        }
        string(size_t n, char ch)
            :_str(new char[n + 1])
             ,_size(n)
             ,_capacity(n)
        {
            size_t i = 0;
            for (i = 0; i < n; i ++ )
                _str[i] = ch;
            _str[i] = '\0';
        }

        ~string()
        {
            delete[] _str;
            _str = nullptr;
            _size = _capacity = 0;
        }

        string(const string& s)
        {
            string tmp(s.c_str());
            swap(tmp);
        }
        Ref operator=(string s)
        {
            swap(s);
            return *this;
        }
        
        char& operator[](size_t pos)
        {
            assert(pos >= 0 && pos < _size);
            return _str[pos];
        }
        const char& operator[](size_t pos) const 
        {
            assert(pos >= 0 && pos < _size);
            return _str[pos];
        }

        void reserve(size_t n)
        {
            if (n > _capacity)
            {
                char* tmp = new char[n + 1];
                strcpy(tmp, _str);
                delete[] _str;

                _str = tmp;
                _capacity = n;
            }
        }
        void resize(size_t n, char ch = char())
        {
            if (n > _size)
            {
                reserve(n);
                for (size_t i = _size; i < n; i ++ )
                    _str[i] = ch;
                _size = n;
                _str[n] = '\0';
            }
            else 
            {
                _str[n] = '\0';
                _size = n;
            }
        }

        void push_back(const char& ch)
        {
            if (_size == _capacity)
                reserve(_capacity == 0 ? 4 : _capacity * 2);

            _str[_size ++ ] = ch;
            _str[_size] = '\0';
        }

        iterator insert(size_t pos, const char& ch, size_t n = 0)
        {
            assert(pos >= 0 && pos <= _size);
            if (n == 0 || n == 1)
            {
                reserve(_size + 1);
                size_t end = _size;
                while (end > pos)
                {
                    _str[end] = _str[end - 1];
                    end -- ;
                }
                _str[pos] = ch;
                _size ++ ;
                _str[_size] = '\0';
            }
            else 
            {
                reserve(_size + n);

                size_t end = _size + n - 1;
                while (end - n > pos)
                {
                    _str[end] = _str[end - n];
                    end -- ;
                }

                size_t tmp = pos;
                for (size_t i = 0; i < n; i ++ )
                    _str[tmp ++ ] = ch;

                _size += n;
                _str[_size] = '\0';
            }
            return _str + pos;
        }
        iterator insert(size_t pos, const char* str)
        {
            assert(pos <= _size);

            size_t len = strlen(str);
            reserve(_size + len);

            size_t end = _size + len - 1;
            while (end - len > pos)
            {
                _str[end] = _str[end - len];
                end -- ;
            }

            strncpy(_str + pos, str, len);
            _size += len;
            _str[_size] = '\0';

            return _str + pos;
        }

        iterator erase(size_t pos, size_t n = npos)
        {
            assert(pos < _size);

            if (n == npos)
            {
                _str[pos] = '\0';
                return _str + pos;
            }
            else 
            {
                size_t begin = pos + n;
                while (begin < _size)
                {
                    _str[begin - n] = _str[begin];
                    begin ++ ;
                }
                _size -= n;
                _str[_size] = '\0';

                return _str + pos;
            }
        }

        void append(size_t n, char ch)
        {
            reserve(_size + n);

            for (size_t i = 0; i < n; i ++ )
                push_back(ch);
        }
        void append(const char* str)
        {
            size_t len = strlen(str);
            reserve(_size + len);

            strcpy(_str + _size, str);
            _size += len;
        }
        void append(const string& s) { append(s.c_str()); }

        bool operator==(const string& s) const 
        {
            return !strcmp(_str, s.c_str());
        }
        bool operator!=(const string& s) const 
        {
            return !(*this == s);
        }

        Ref operator+=(const char* str)
        {
            size_t len = strlen(str);
            for (size_t i = 0; i < len; i ++ )
                push_back(str[i]);
            return *this;
        }
        Ref operator+=(const char& ch)
        {
            push_back(ch);
            return *this;
        }
        Ref operator+=(const string& s)
        {
            *this += s.c_str();
            return *this;
        }

        size_t find(const char* str, size_t pos = 0) const 
        {
            assert(pos < _size);
            
            const char* ret = strstr(_str + pos, str);

            if (ret == nullptr)
                return npos;
            return ret - _str;
        }

        void clear()
        {
            *_str = '\0';
            _size = 0;
        }

        size_t size() const { return _size; }
        size_t capacity() const { return _capacity; }
        bool empty() const { return _size == 0; }

    private:
        char* _str;
        size_t _size;
        size_t _capacity;

        const static size_t npos = -1;
    };

    ostream& operator<<(ostream& out, const string& s)
    {
        for (size_t i = 0; i < s.size(); i ++ )
            out << s[i];
        return out;
    }

    istream& operator>>(istream& in, string& s)
    {
        s.clear();
        char buff[32];
        char ch;
        ch = in.get();

        size_t i = 0;
        while (ch != ' ' && ch != '\n')
        {
            buff[i ++ ] = ch;
            if (i == 32)
            {
                s += buff;
                i = 0;
            }
            ch = in.get();
        }

        buff[i] = '\0';
        s += buff;

        return in;
    }

        void test1()
    {
        string s1("hello linux!!!");
        s1.push_back('a');
        s1.push_back('b');
        s1.push_back('c');
        s1.push_back('d');
        s1.push_back('e');
        s1.push_back('f');

        for (size_t i = 0; i < s1.size(); i ++ )
        {
            cout << s1[i] << ' ';
        }
        cout << endl;

        string s2(s1);
        s2.append(" hello");
        s2.append(6, 'o');
        s2.append(s1);
        cout << s2 << endl;

        string s3("opopop");
        cout << s3 << endl;
        s3 = s2;
        cout << s3 << endl;
    }

    void test2()
    {
        string s1("hello linux !!!");
        cout << s1 << endl;
        s1.insert(0, "pppppp");
        cout << s1 << endl;

        string s2(s1);
        cout << s2 << endl;
        s2.erase(0, 6);
        cout << s2 << endl;
        s2.insert(5, " nishishabima");
        cout << s2 << endl;

        string s3;
        s3 = s2;
        cout << s3 << endl;
        s3 += " gubaby";
        cout << s3 << endl;
        s3.erase(1);
        cout << s3 << endl;
    }

    void test3()
    {
        string s1(10, 'a');
        cout << s1 << endl;
        s1.insert(6, "hello linux !!! ");
        cout << s1 << endl;
        s1.erase(s1.find("linux", 2), 6);
        cout << s1 << endl;
    }
}
